package com.wanxi.dangdang.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanxi.dangdang.common.UserType;
import com.wanxi.dangdang.mapper.AdminMapper;
import com.wanxi.dangdang.model.dto.AdminDelAllDto;
import com.wanxi.dangdang.model.dto.AdminFindPageDto;
import com.wanxi.dangdang.model.pojo.Admin;
import com.wanxi.dangdang.model.pojo.Role;
import com.wanxi.dangdang.model.vo.AdminFindByIdVo;
import com.wanxi.dangdang.model.vo.AdminFindPageVo;
import com.wanxi.dangdang.model.vo.AdminRoleVo;
import com.wanxi.dangdang.model.vo.AdminVo;
import com.wanxi.dangdang.service.AdminService;
import com.wanxi.dangdang.service.RoleService;
import lombok.extern.log4j.Log4j;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import com.wanxi.dangdang.util.JwtTokenUtil;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author: 吕永杰
 * @Description:
 * @Date: Created in 2021/9/1
 * @Version: 1.0
 */
@Service
@Slf4j
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private JwtTokenUtil jwtTokenUtil;

    @Autowired
    private AdminService adminService;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private Role role;

    @Value("${admin.rePassword}")
    private String rePassword;


    @Override
    public String checkAccount(String account) {
        return adminMapper.checkAccount(account);
    }

    @Override
    public void add(Admin admin) {
        admin.setPassword(passwordEncoder.encode(admin.getPassword()));
        adminMapper.add(admin);
        log.info("id为"+admin.getSuperManagerId()+"的超级管理员添加了一个用户");
    }

    @Override
    public AdminFindByIdVo findById(Long id) {
        Admin admin = adminMapper.findById(id);
        AdminFindByIdVo adminFindByIdVo = new AdminFindByIdVo();
        BeanUtils.copyProperties(admin,adminFindByIdVo);
        return adminFindByIdVo;
    }

    @Override
    public PageInfo<AdminFindPageVo>  findPage(AdminFindPageDto adminFindPageDto) {
        PageHelper.startPage(adminFindPageDto.getPageNum(), adminFindPageDto.getPageSize());
        List<AdminFindPageVo> adminVos = adminMapper.findPage(adminFindPageDto);
        for (AdminFindPageVo adminVo : adminVos) {
            List<Role> roles = adminVo.getRoles();
            if(roles.size()==0){
                roles.add(this.role);
            }
        }
        PageInfo<AdminFindPageVo> pageInfo = new PageInfo(adminVos);
        return pageInfo;
    }

    @Override
    public void updateById(Admin admin) {
        adminMapper.updateById(admin);
        log.info("id为"+admin.getSuperManagerId()+"的超级管理员修改了id为"+admin.getId()+"的管理员");
    }

    @Override
    public void deleteById(Long id) {
        adminMapper.deleteById(id);
        adminMapper.deleteAdminRoleById(id);
    }

    @Override
    public void updateRolesById(AdminRoleVo adminRoleVo) {
        adminMapper.deleteAdminRoleById(adminRoleVo.getId());
        adminMapper.updateRolesById(adminRoleVo);
    }

    @Override
    public String loginByPass(Admin admin) {
        Admin admin1;
        String account = admin.getAccount();
        if(account==null)
            return null;
        String password = admin.getPassword();
        if(password==null)
            return null;
        if((admin1=adminMapper.loadUserByUsername(admin.getAccount()))!=null){
            if (passwordEncoder.matches(admin.getPassword(),admin1.getPassword()))
            {
                log.info(account+"登录成功！");
                return jwtTokenUtil.generateToken(admin.getAccount(),admin1.getId(), UserType.ADMIN);
            }

        }
        return null;
    }

    @Override
    public AdminVo loadUserByUsername(String accoutn) {
        return adminMapper.loadUserByUsername(accoutn);
    }

    @Override
    public void rePassword(Long id) {
        Admin admin = new Admin();
        admin.setId(id);
        admin.setPassword(passwordEncoder.encode(rePassword));
        adminMapper.rePassword(admin);
    }

    @Override
    @Transactional
    public void delAll(AdminDelAllDto adminDelAllDto) {
        for (Long id : adminDelAllDto.getIds()) {
            adminService.deleteById(id);
        }
        throw new RuntimeException();
    }

}

